package com.stedu.mall.adminservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.adminservice.mapper.AdminMapper;
import com.stedu.mall.common.bean.Admin;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
   @Autowired
   private AdminMapper adminMapper;
    @Override
    public Admin longin(String username, String password) throws SteduException {
     Admin admin = adminMapper.selectByUsername(username) ;
     if(admin.getStatus()==1){
         throw new SteduException("账号已被禁用");
     }
     if(admin==null){
         throw new SteduException("用户名错误");
     }
        //MD5+盐加密
      String md5Pwd= SecureUtil.md5(SecureUtil.md5(password+admin.getSalt()));
     if(!md5Pwd.equals(admin.getPassword()))
     {
         throw new SteduException("密码错误");
     }

        return admin;
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public boolean update(Admin admin) {

        return adminMapper.update(admin) == 1;
    }


    @Override
    public boolean chgPwd(String oldPwd, String newPwd, Integer id) {
     Admin admin = adminMapper.selectById(id);
     String salt = admin.getSalt();
        String md5InputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPwd + salt));
        if(!admin.getPassword().equals(md5InputOldPwd)){
            new SteduException("原密码输入错误，请确认后重新输入");
        }
        //对新密码加盐加密
        String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPwd+salt));
        //修改密码
        admin=new Admin();
        admin.setId(id);
        admin.setPassword(md5NewPwd);
        return  adminMapper.update(admin)==1;
    }

    @Override
    public PageInfo<Admin> selectByCondition(Admin condition, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        //查询
        List<Admin> admin = adminMapper.selectByCondtion(condition);

        PageInfo<Admin> pageInfo = new PageInfo<>(admin);

        return pageInfo;
    }

    @Override
    public boolean updatePwd(Admin admin) {
        String pwd= admin.getPassword();
        String salt = admin.getSalt();
        String md5Pwd= SecureUtil.md5(SecureUtil.md5(pwd+admin.getSalt()));
        admin.setPassword(md5Pwd);
       return adminMapper.update(admin)==1;
    }

    @Override
    public boolean insert(Admin admin) throws SteduException {
        if(adminMapper.selectByUsername(admin.getUsername()) !=null){
            throw new SteduException("用户已存在，无法注册");
        }
        //生成盐
        String  salt= RandomUtil.randomString(8);
        admin.setSalt(salt);
        //对密码进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(admin.getPassword()+salt));
        admin.setPassword(md5Pwd);
        //设置状态
        admin.setStatus(0);
        //添加管理员
        return adminMapper.insert(admin)==1;
    }
}
